package cn.engage.org.controller;

import cn.engage.basic.config.BussinessException;
import cn.engage.basic.util.ExcelUtil;
import cn.engage.org.domain.Department;
import cn.engage.org.domain.ShopAuditLog;
import cn.engage.org.service.ShopService;
import cn.engage.org.domain.Shop;
import cn.engage.org.query.ShopQuery;
import cn.engage.basic.util.AjaxResult;
import cn.engage.basic.util.PageList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public ShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        if (shop.getId() == null){
            shopService.add(shop);
        }else {
            shopService.update(shop);
        }
        return AjaxResult.me().success();
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        shopService.delete(id);
        return AjaxResult.me().success();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
       shopService.patchDelete(ids);
       return AjaxResult.me().success();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findById(@PathVariable("id") Long id){
        return shopService.findByIdOne(id);
    }

    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> queryByPage(@RequestBody ShopQuery query) {
        return shopService.queryByPage(query);
    }

    /* 店铺入驻*/
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        shopService.settlement(shop);
        return new AjaxResult().me().success();
    }
    /**
     * 店铺入驻审核通过
     */
    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) throws MessagingException {
        if (StringUtils.isBlank(shopAuditLog.getNote())){
            throw new BussinessException("日志不能为空");
        }
        shopService.auditPass(shopAuditLog);

        return AjaxResult.me().success();
    }
    /**
     * 店铺入驻审核驳回
     */
    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog) throws MessagingException {
        if (StringUtils.isBlank(shopAuditLog.getNote())){
            throw new BussinessException("日志不能为空");
        }
        shopService.auditReject(shopAuditLog);

        return AjaxResult.me().success();
    }

    /**
     * 审核通过通过邮件激活
     */
    @GetMapping("/active/{id}")
    public AjaxResult active(@PathVariable("id") Long id) throws MessagingException {
        shopService.active(id);

        return AjaxResult.me().success();
    }
    /**
     * 审核驳回通过邮件修改店铺信息
     */
    @PostMapping("/faild")
    public AjaxResult faild(@RequestBody Shop shop) throws MessagingException {
        shopService.faild(shop);
        return AjaxResult.me().success();
    }

    /**
     * 文件导出
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        // 导出
        ExcelUtil.exportExcel(shops, null, "店铺信息",Shop.class,"店铺名称.xlxs",response);
    }
    /**
     * 文件导入
     */
    @PostMapping("/import/excel")
    public AjaxResult importExcel(@RequestPart("file") MultipartFile file){
        // 导入
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shopService.batchAdd(shops);
        return AjaxResult.me().success();
    }
    /**
     * 前端报表
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        return shopService.echarts();
    }
}
